package net.sudot.chess.business.service.system;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
import net.sudot.chess.business.model.SystemConfig;
import net.sudot.chess.business.wechat.api.WechatApiService;
import net.sudot.chess.exception.BusinessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;

/**
 * 微信素材处理策略
 *
 * @author tangjialin on 2018-03-26.
 */
@Service
public class WechatMediaProcessStrategy implements SystemConfigProcessStrategy {
    @Resource
    private WechatApiService wechatApiService;

    @Override
    public String process(SystemConfig config, Object value) {
        WxMpMaterial material = new WxMpMaterial();
        material.setFile((File) value);
        try {
            WxMpMaterialUploadResult result = wechatApiService.getMaterialService().materialFileUpload(WxConsts.MaterialType.IMAGE, material);
            return result.getMediaId();
        } catch (Exception e) {
            throw new BusinessException("微信素材上传失败", e);
        }
    }
}
